package com.android.gufengmall.login;

import android.app.ActivityOptions;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.transition.Explode;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.cardview.widget.CardView;
import androidx.core.app.ActivityOptionsCompat;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.MainActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.admin.AdminActivity;
import com.android.gufengmall.bean.UserBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.UserBeanDao;
import com.android.gufengmall.utils.LogUtils;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

public class LoginActivity extends BaseActivity {

    private static final String TAG = "LoginActivity";

    private EditText mEtUsername;
    private EditText mEtPassword;
    private Button mBtGo;
    private CardView mCardView;
    private FloatingActionButton mFloatingActionButton;
    private App mMyApp;
    private DaoSession mDaoSession;
    private UserBeanDao mUserDao;
    private TextView mTvAdmin;
    private CheckBox mChkAutoLogin;
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    private int mLoginFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initData();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            initListener();
        }
    }

    private void initData() {
        mSharedPreferences = getSharedPreferences("autoLogin", MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();

        mMyApp = (App) getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mUserDao = mDaoSession.getUserBeanDao();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initListener() {
        mFloatingActionButton.setOnClickListener(v -> {
            getWindow().setExitTransition(null);
            getWindow().setEnterTransition(null);
            ActivityOptions options =
                    ActivityOptions.makeSceneTransitionAnimation(LoginActivity.this, mFloatingActionButton, mFloatingActionButton.getTransitionName());
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class), options.toBundle());
        });
        mBtGo.setOnClickListener(v -> {
            List<UserBean> users = mUserDao.loadAll();
            if (users.isEmpty()) {
                showToast("登录失败，清先注册账户");
            }
            String edtUser = mEtUsername.getText().toString().trim();
            String edtPass = mEtPassword.getText().toString().trim();
            if (edtUser.isEmpty() || edtPass.isEmpty()) {
                showToast("登录失败，账号密码为空");
            } else {
                mLoginFlag = 0;
                for (int i = 0; i < users.size(); i++) {
                    UserBean user = users.get(i);
                    if (user.getUserName().equals(edtUser) && user.getUserPass().equals(edtPass)) {
                        mLoginFlag = 1;
                    } else if (user.getUserName().equals(edtUser) && !user.getUserPass().equals(edtPass)) {
                        mLoginFlag = 2;
                    }
                }
                if (mLoginFlag == 1) {
                    Explode explode = new Explode();
                    explode.setDuration(500);
                    getWindow().setExitTransition(explode);
                    getWindow().setEnterTransition(explode);
                    ActivityOptionsCompat oc2 = ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this);
                    Intent i2 = new Intent(LoginActivity.this, MainActivity.class);
                    i2.putExtra("username", edtUser);
                    startActivity(i2, oc2.toBundle());
                } else if (mLoginFlag == 2) {
                    showToast("登录失败，请检查账号密码");
                }
                mEditor.putBoolean("autoLogin", mChkAutoLogin.isChecked());
                mEditor.putString("username", edtUser);
                mEditor.apply();
            }

        });
        mTvAdmin.setOnClickListener(v -> {
            String edtUser = mEtUsername.getText().toString().trim();
            String edtPass = mEtPassword.getText().toString().trim();
            if (edtUser.equals("admin") && edtPass.equals("123456")) {
                startActivity(new Intent(LoginActivity.this, AdminActivity.class));
            } else {
                showToast("登录失败，请检查账号密码");
            }
        });
    }

    private void initView() {
        mEtUsername = findViewById(R.id.et_username);
        mEtPassword = findViewById(R.id.et_password);
        mTvAdmin = findViewById(R.id.tv_admin);
        mChkAutoLogin = findViewById(R.id.chk_auto_login);
        mBtGo = findViewById(R.id.bt_go);
        mCardView = findViewById(R.id.cv);
        mFloatingActionButton = findViewById(R.id.fab);
    }
}